# -*- coding: utf-8 -*-
# @Time    : 2023/12/06 17:25
# @Author  : Jonny
# @File    : LuckConstant.py
# @Describe: 常量类

import os
import sys

class LuckConstant(object):
    LOG_DIR = "../works/logs"
    LOG_FILE = "Luck.log"
    DB_DIR = "config"
    DB_NAME = "Luck.db"
    CONFIG_FILE = "config/LuckConfig.json"
    
    @staticmethod
    def get_user_data_dir():
        """获取用户数据目录，确保数据库文件持久化"""
        if sys.platform == "win32":
            # Windows系统
            app_data = os.environ.get('APPDATA')
            if app_data:
                user_data_dir = os.path.join(app_data, 'LuckSMS')
            else:
                user_data_dir = os.path.join(os.path.expanduser('~'), 'AppData', 'Roaming', 'LuckSMS')
        elif sys.platform == "darwin":
            # macOS系统
            user_data_dir = os.path.join(os.path.expanduser('~'), 'Library', 'Application Support', 'LuckSMS')
        else:
            # Linux系统
            user_data_dir = os.path.join(os.path.expanduser('~'), '.config', 'LuckSMS')
        
        # 确保目录存在
        if not os.path.exists(user_data_dir):
            os.makedirs(user_data_dir, exist_ok=True)
        
        return user_data_dir
    
    @staticmethod
    def get_db_path():
        """获取数据库文件的完整路径"""
        if getattr(sys, 'frozen', False):
            # 如果是PyInstaller打包的exe，将数据库文件放在exe同级目录
            exe_dir = os.path.dirname(sys.executable)
            db_path = os.path.join(exe_dir, LuckConstant.DB_NAME)
            
            # 如果exe同级目录中没有数据库文件，从模板复制
            if not os.path.exists(db_path):
                template_db_path = os.path.join(sys._MEIPASS, 'config_template', LuckConstant.DB_NAME)
                if os.path.exists(template_db_path):
                    import shutil
                    shutil.copy2(template_db_path, db_path)
            
            return db_path
        else:
            # 开发环境，使用原来的路径
            script_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
            return os.path.join(script_dir, LuckConstant.DB_DIR, LuckConstant.DB_NAME)
    
    @staticmethod
    def get_config_path():
        """获取配置文件的完整路径"""
        if getattr(sys, 'frozen', False):
            # 如果是PyInstaller打包的exe，将配置文件放在exe同级目录
            exe_dir = os.path.dirname(sys.executable)
            config_path = os.path.join(exe_dir, 'LuckConfig.json')
            
            # 如果exe同级目录中没有配置文件，从模板复制
            if not os.path.exists(config_path):
                template_config_path = os.path.join(sys._MEIPASS, 'config_template', 'LuckConfig.json')
                if os.path.exists(template_config_path):
                    import shutil
                    shutil.copy2(template_config_path, config_path)
            
            return config_path
        else:
            # 开发环境，使用原来的路径
            script_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
            return os.path.join(script_dir, LuckConstant.CONFIG_FILE)

    AUTH_INFO = "auth_info"
    PAGE_SIZE = "page_size"
    PAGE_NO = "page_no"
    DYNAMIC_CONDITION = "dynamicCondition"
    CODE_SUCCESS = 0
    CODE_FAIL = 1
    MSG_NO_AUTH = "认证失败"
    CODE_NO_AUTH = 401
    MSG_SUCCESS = "操作成功"
    MSG_FAIL = "操作失败"

    ACCESS_TOKEN = "Authorization"
    ACCESS_TOKEN_OLD = "access_token"
